/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wli.web.java.db;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * @author wli
 */
@ManagedBean
@SessionScoped
public class VocabularyController implements Serializable {

    private DataModel vocabularyList;
    private VocabularyHelper vchelper;
    private Vocabulary current;
    private String first_word;

    /** Creates a new instance of VocabularyController */
    public VocabularyController() {
        this.vchelper = new VocabularyHelper();
    }

    public VocabularyController(String word) {
        this.vchelper = new VocabularyHelper();
        first_word = word;
    }

    public DataModel getVocabularyList() {
        if (vocabularyList == null) {
            //System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>VocabularyController:getVocabularyList>>>>>>>>>>>>>>>>>>>>>>>>");
            vocabularyList = new ListDataModel(vchelper.getVocabularyList());
        }
        return vocabularyList;
    }

    void recreateModel() {
        vocabularyList = null;
    }

    public String prepareView() {
        if (current != null) {
            //System.out.println("====================VocabularyController:prepareView========AAAAAAAAAAAAAAAAAAA========" + current.getWord());
            //System.out.println("====================VocabularyController:prepareView========AAAAAAAAAAAAAAAAAAA========" + current.getDefinition());
        }
        current = (Vocabulary) getVocabularyList().getRowData();
        System.out.println("====================VocabularyController:prepareView========BBBBBBBBBBBBBBBB========" + current.getWord());
        //System.out.println("====================VocabularyController:prepareView========BBBBBBBBBBBBBBBB========" + current.getDefinition());
        return "index3";
    }

    public String prepareViewout() {
        current = (Vocabulary) getVocabularyList().getRowData();
        //System.out.println("====================VocabularyController:prepareView================" + current.getWord());
        //current = current.getWord();
        //System.out.println("====================VocabularyController:prepareView================" + current.getWord());
        //System.out.println("====================VocabularyController:prepareView================" + current.getDefinition());
        return "index3";
    }

    public Vocabulary getSelected() {
        if (current == null) {
            //System.out.println(">>>>>>>>>>>>>>>>>>>>VocabularyController:getSelected>>>>>>>AAAAAAAAAAAAAAAAA");
            current = vchelper.getVocabularyByID(first_word);
            //System.out.println(">>>>>>>>>>>>>>>>>>>>>VocabularyController:getSelected>>>>>>BBBBBBBBBBBBBBBBB");
        }
        //System.out.println(">>>>>>>>>>>>>>>>>>>>>VocabularyController:getSelectedCCCCCCCCCCCCCC>>>>>>>>>>>>>>>>>>" + current.getWord());
        first_word = current.getWord();
        return current;
    }

    public String updateVocabulary() {
        //System.out.println("====================VocabularyController:prepareView================" + current.getWord());
        vchelper.updateVocabulary(current.getWord(), current.getDefinition());
        System.out.println("====================VocabularyController:prepareView================" + current.getWord());
        System.out.println("====================VocabularyController:prepareView================" + current.getDefinition());
        return "index3";
    }
}
